home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / TELECOMM / PCCP047 / XMCRC1KS.C < prev    next >
Text File  |  1992-07-10  |  4KB  |  239 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include"port.h"
  13.  
  14. #define NAK 21
  15. #define ACK 6
  16. #define SOH 1
  17. #define STX 2
  18. #define EOT 4
  19. #define CAN 24
  20.  
  21. sendchar(c)
  22.     unsigned char c;
  23.     {
  24.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)));
  25.     outp(basereg, c);
  26.     }
  27.  
  28. int follow;
  29.  
  30. int rcharto(ticks)
  31.     int ticks;
  32.     {
  33.     long tstamp, tstamp1, dayofticksp;
  34.     int c;
  35.     _bios_timeofday(_TIME_GETCLOCK, &tstamp);
  36.     dayofticksp=0;
  37.     while(1)
  38.         {
  39.         if(kbhit())
  40.             getch();
  41.         if(_bios_timeofday(_TIME_GETCLOCK, &tstamp1))
  42.             dayofticksp+=20*60*60*24;
  43.         if(tstamp1+dayofticksp-tstamp>ticks)
  44.             return(-1); /* NOTE: This is an INT!!! */
  45.         if(follow!=index)
  46.             {
  47.             c=buf[follow++];
  48.             follow=follow%TBUFSIZ;
  49.             return(c);
  50.             }
  51.         }
  52.     }
  53.  
  54. int calccrc(ptr, count)
  55.     char *ptr;
  56.     int count;
  57.     {
  58.     int crc, i;
  59.     crc = 0;
  60.     while(--count >= 0)
  61.         {
  62.         crc = crc ^ (int)*ptr++ << 8;
  63.         for(i = 0; i < 8; ++i)
  64.             if(crc & 0x8000)
  65.                 crc = crc << 1 ^ 0x1021;
  66.             else
  67.                 crc = crc << 1;
  68.         }
  69.     return (crc & 0xFFFF);
  70.     }
  71.  
  72. unsigned char block[1024];
  73.  
  74. sblock(blockn)
  75.     int blockn;
  76.     {
  77.     unsigned char c;
  78.     unsigned short crc, rcrc;
  79.     int i;
  80.     crc=calccrc(block, 1024);
  81.     sendchar(STX);
  82.     sendchar(blockn);
  83.     sendchar((blockn^0xff)&0xff);
  84.     for(i=0;i<1024;++i)
  85.         sendchar(block[i]);
  86.     sendchar((crc>>8)&0xff);
  87.     sendchar(crc&0xff);
  88.     }
  89.  
  90. unsigned char shortblock[128];
  91.  
  92. ssblock(blockn)
  93.     int blockn;
  94.     {
  95.     unsigned char c;
  96.     unsigned short crc, rcrc;
  97.     int i;
  98.     crc=calccrc(shortblock, 128);
  99.     sendchar(SOH);
  100.     sendchar(blockn);
  101.     sendchar((blockn^0xff)&0xff);
  102.     for(i=0;i<128;++i)
  103.         sendchar(shortblock[i]);
  104.     sendchar((crc>>8)&0xff);
  105.     sendchar(crc&0xff);
  106.     }
  107.  
  108. quit()
  109.     {
  110.     cleanup(0);
  111.     exit(99);
  112.     }
  113.  
  114. main(argc, argv)
  115.     int argc;
  116.     char **argv;
  117.     {
  118.     int i, j, k, l, infd, ok, c;
  119.     unsigned char *blkptr;
  120.     unsigned char blocknum;
  121.     long nbytes;
  122.     index=follow=0;
  123.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  124.     printf("xmodem crc 1k send of %s.\n", argv[4]);
  125.     if(argc!=5)
  126.         {
  127.         printf("USAGE: xmodemr <comnum> <bps> <stopbits> <file pathname>\n");
  128.         exit(1);
  129.         }
  130.     if((infd=open(argv[4], O_RDONLY|O_BINARY))==-1)
  131.         {
  132.         printf("Error opening file %s.\n", argv[4]);
  133.         exit(2);
  134.         }
  135.     comnum=atoi(argv[1])-1;
  136.     speed=atoi(argv[2]);
  137.     databits='8';
  138.     parity='n';
  139.     stopbits=argv[3][0];
  140.     setport();
  141.     signal(SIGINT, quit);
  142.     readset();
  143.     setup();
  144.     nbytes=0;
  145.     if(rcharto(2000)!='C')
  146.         {
  147.         printf("Spurrious char or no C in 100 seconds.\n");
  148.         cleanup(0);
  149.         exit(10);
  150.         }
  151.     blocknum=1;
  152.     while(1)
  153.         {
  154.         if((j=read(infd, block, 1024))==0)
  155.             {
  156.             printf("\nEnd of file.\n");
  157.             sendchar(EOT);
  158.             do
  159.                 c=rcharto(300);
  160.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  161.             if(c!=ACK)
  162.                 {
  163.                 printf("No ACK of EOT.\n");
  164.                 cleanup(0);
  165.                 exit(13);
  166.                 }
  167.             else
  168.                 {
  169.                 printf("Successful.\n");
  170.                 cleanup(0);
  171.                 exit(0);
  172.                 }
  173.             }
  174.         for(c=j;c<1024;c++)
  175.             block[c]=26;
  176.         if(j>896)
  177.             {
  178.             i=0;
  179.             do
  180.                 {
  181.                 printf("\nSending block %d. ", blocknum);
  182.                 sblock(blocknum);
  183.                 do
  184.                     c=rcharto(200);
  185.                 while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  186.                 }
  187.             while((c==NAK)&&(i++<10));
  188.             if(c==ACK)
  189.                 {
  190.                 blocknum++;
  191.                 nbytes+=1024;
  192.                 printf("Successful. Bytes so far: %ld", nbytes);
  193.                 }
  194.             }
  195.         else
  196.             {
  197.             for(k=0;k<(j+128);k+=128)
  198.                 {
  199.                 i=0;
  200.                 do
  201.                     {
  202.                     for(l=0;l<128;++l)
  203.                         shortblock[l]=block[k+l];
  204.                     printf("\nSending block %d. ", blocknum);
  205.                     ssblock(blocknum);
  206.                     do
  207.                         c=rcharto(200);
  208.                     while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  209.                     }
  210.                 while((c==NAK)&&(i++<10));
  211.                 if(c!=ACK)
  212.                     break;
  213.                 else
  214.                     {
  215.                     blocknum++;
  216.                     nbytes+=128;
  217.                     printf("Successful. Bytes so far: %ld", nbytes);
  218.                     }
  219.                 }
  220.             }
  221.         if(c!=ACK)
  222.             if(c==NAK)
  223.                 {
  224.                 printf("\nRetry limit exceeded.\n");
  225.                 cleanup(0);
  226.                 exit(14);
  227.                 }
  228.             else
  229.                 {
  230.                 printf("\nSpurrious character hex %02x; ACK or NAK expected.\n", c);
  231.                 cleanup(0);
  232.                 exit(11);
  233.                 }
  234.         }
  235.     printf("Programming error; fell through end; see code.\n");
  236.     cleanup(0);
  237.     exit(12);
  238.     }
  239.